package com.magnificent.atlantis.client.ui.creator.tasktreetable.data;

import com.magnificent.atlantis.client.data.task.creator.AbstractCreatorTask;

import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * User: Babanin
 * Date: 24.12.2008
 * Time: 1:50:50
 * <p/>
 * Feci quod potui, faciant meliora potentes.
 */
public class CreatorTableNode {
    private CreatorTableNode parent;
    private AbstractCreatorTask abstractTask;
    private List<CreatorTableNode> children;

    public CreatorTableNode() {
        abstractTask = null;
        children = new ArrayList<CreatorTableNode>();
    }

    public CreatorTableNode(AbstractCreatorTask abstractTask, CreatorTableNode parent) {
        this.parent = parent;
        this.abstractTask = abstractTask;
        children = new ArrayList<CreatorTableNode>();
    }

    public List<CreatorTableNode> getChildren() {
        return children;
    }

    public int getChildCount() {
        return children.size();
    }

    public CreatorTableNode getParent() {
        return parent;
    }

    public void setParent(CreatorTableNode parent) {
        this.parent = parent;
    }

    public AbstractCreatorTask getTask() {
        return abstractTask;
    }

    public void setChildren(List<CreatorTableNode> children) {
        this.children = children;
    }

    public boolean isMain() {
        return (parent != null) && parent.isRoot();
    }

    public boolean isRoot() {
        return (abstractTask == null) && (parent == null);
    }

    public TreePath getTreePath() {
        CreatorTableNode node = this;
        List<Object> treePath = new ArrayList<Object>();
        while (node != null) {
            treePath.add(node);
            node = node.getParent();
        }

        if (treePath.size() == 0) {
            return new TreePath(this);
        } else {
            Collections.reverse(treePath);
            return new TreePath(treePath.toArray());
        }
    }

    public static Object[] getParentPath(CreatorTableNode node) {
        List<Object> treePath = new ArrayList<Object>();
        while (node.getParent() != null) {
            treePath.add(node.getParent());
            node = node.getParent();
        }

        if (treePath.size() == 0) {
            treePath.add(node);
        } else {
            Collections.reverse(treePath);
        }

        return treePath.toArray();
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CreatorTableNode)) return false;

        CreatorTableNode that = (CreatorTableNode) o;

        return !(abstractTask != null ? !abstractTask.equals(that.abstractTask) : that.abstractTask != null);
    }

    public int hashCode() {
        int result;
        result = (parent != null ? parent.hashCode() : 0);
        result = 31 * result + (abstractTask != null ? abstractTask.hashCode() : 0);
        return result;
    }
}
